package com.fhz.exam001;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.fhz.exam001.adapter.MyAdapter;
import com.fhz.exam001.fragment.IndexFragment;
import com.fhz.exam001.fragment.ListFragment;
import com.fhz.exam001.fragment.MineFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private RadioGroup rg;
    private RadioButton index;
    private RadioButton ls;
    private RadioButton me;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.vp);
        rg = (RadioGroup) findViewById(R.id.rg);
        index = (RadioButton) findViewById(R.id.index);
        ls = (RadioButton) findViewById(R.id.ls);
        me = (RadioButton) findViewById(R.id.me);

        List<Fragment>  list  = new ArrayList<>();
        //ctrl + d 快速复制当前行
        list.add(new IndexFragment());
        list.add(new ListFragment());
        list.add(new MineFragment());

        MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager(), list);

        vp.setAdapter(myAdapter);

        //用两个监听器,让vp和btn 关联起来

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.index:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.ls:
                        vp.setCurrentItem(1);
                        break;
                    case  R.id.me:
                        vp.setCurrentItem(2);
                        break;
                }
            }
        });

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        index.setChecked(true);
                        break;
                    case 1:
                        ls.setChecked(true);
                        break;
                    case 2:
                        me.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });





    }
}